package com.project;

import com.google.gson.Gson;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getAirlineByFid")
public class GetAirlineByFidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String fid = request.getParameter("fid");
        DB_Airline dbAirline = new DB_Airline();
        List<String> conditions = new ArrayList<>();
        conditions.add("fid=" + fid);
        List<String> airlineResults = dbAirline.find(conditions);

        List<Map<String, String>> airlines = new ArrayList<>();
        for (String airline : airlineResults) {
            String[] parts = airline.split(",");
            Map<String, String> map = new HashMap<>();
            map.put("aid", parts[0]);
            map.put("fid", parts[1]);
            map.put("position", parts[3]);
            airlines.add(map);
        }

        response.getWriter().print(new Gson().toJson(airlines));
    }
}